/* $Id: adcread.c,v 1.1 2003/12/09 22:43:28 GrosbaJ Exp $ */
#include <p18f4550.h>
#include <adccidei4550.h>

/********************************************************************
*    Function Name:  ReadADC                                        *
*    Return Value:   int, A/D result                                *
*    Parameters:     void                                           *
*    Description:    This routine reads the ADRESL and ADRESH       *
*                    and returns these as a long.                   *
********************************************************************/
int ReadADC(void)
{
  union ADCResult i; // A union is used to read the
                     // A/D result due to issues with
                     // handling long variables

  i.br[0] = ADRESL;  // Read ADRESL into the lower byte
  i.br[1] = ADRESH;  // Read ADRESH into the high byte

  return (i.lr);     // Return the long variable
}

/****************************************************************
*    Function Name:  BusyADC                                    *
*    Return Value:   char: 1 - A/D busy, 0 - Done converting    *
*    Parameters:     void                                       *
*    Description:    This routine checks the GO bit to see if   *
*                    the A/D conversion is done.                *
****************************************************************/
char BusyADC(void)
{
  /* If the GO bit is set then A/D is busy converting
   * Otherwise A/D is done converting
   */
  return(ADCON0bits.GO_DONE);

}

/********************************************************************
*    Function Name:  ConvertADC                                     *
*    Return Value:   void                                           *
*    Parameters:     void                                           *
*    Description:    This routine starts an A/D conversion by       *
*                    setting the GO bit.                            *
********************************************************************/
void ConvertADC(void)
{
  ADCON0bits.GO_DONE = 1;  // Set the GO bit to start a conversion
}

/********************************************************************
*    Function Name:  OpenADC                                        *
*    Return Value:   void                                           *
*    Parameters:     config: bit definitions to configure A/D       *
*                    channel: channel selection                     *
*    Description:    This routine first resets the A/D registers    *
*                    to the POR state and then configures the       *
*                    A/D clock, interrupt, justification, vref      *
*                    source, and I/O.  It also sets the channel.    *
*    Notes:          The bit definitions for config and channel     *
*                    can be found in the adc16.h file.              *
********************************************************************/
void OpenADC( void)
{
// Reset A/D Registers to POR state

//configuracion del adc/////////////////////////
ADCON1=0x0E;
ADCON0=0x00;
ADCON2=0xFE;
ADCON0bits.ADON=1;
/////////////////////

    ADCON0bits.ADON = 1;         // Enable the A/D
}

/************************************************************************
*    Function Name:  CloseADC                                           *
*    Return Value:   void                                               *
*    Parameters:     void                                               *
*    Description:    This routine turns the A/D off and disables        *
*                    the A/D interrupt                                  *
************************************************************************/
void CloseADC(void)
{
  ADCON0bits.ADON = 0;
  PIE1bits.ADIE = 0;
}
